using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Common;
using MyMath;
using SExperiment;

namespace Images
{
    public class DumpInvoke
    {
        public static bool DebugMode = true;

        private static Dictionary<string, Assembly> MathList = new Dictionary<string, Assembly>();

        public static void Init()
        {
            string path = Utility.GetApplicationPath() + "MyMath.dll";
            if (File.Exists(path))
            {
                try
                {
                    File.Delete(path);
                }
                catch
                {
                }
            }
            string path2 = Utility.GetApplicationPath() + "Dump.dll";
            MathList.Clear();
            if (!File.Exists(path2))
            {
                return;
            }
            byte[] array = File.ReadAllBytes(path2);
            int num = 116;
            for (int i = num; i < array.Length; i++)
            {
                array[i] ^= array[i % num];
            }
            int num2 = BitConverter.ToInt32(array, num);
            num += 4;
            for (int j = 0; j < num2; j++)
            {
                int num3 = BitConverter.ToInt32(array, num);
                num += 4;
                byte[] array2 = array.ToList().GetRange(num, num3).ToArray();
                num += num3;
                num3 = BitConverter.ToInt32(array, num);
                num += 4;
                byte[] array3 = array.ToList().GetRange(num, num3).ToArray();
                num += num3;
                for (int k = 0; k < array3.Length; k++)
                {
                    array3[k] ^= array2[j % num2];
                }
                try
                {
                    Assembly assembly = Assembly.Load(array3);
                    Type type = assembly.GetType("MyMath.AnalyseMath");
                    if (!(type == null))
                    {
                        PropertyInfo property = type.GetProperty("Version", typeof(string));
                        object obj2 = Activator.CreateInstance(type);
                        object value = property.GetValue(obj2, null);
                        string key = Convert.ToString(value);
                        MathList.Add(key, assembly);
                    }
                }
                catch (Exception)
                {
                }
            }
            MathList = MathList.OrderBy((KeyValuePair<string, Assembly> p) => p.Key).ToDictionary((KeyValuePair<string, Assembly> p) => p.Key, (KeyValuePair<string, Assembly> o) => o.Value);
        }

        public static Assembly GetDefaultMath()
        {
            if (MathList.Count > 0)
            {
                return MathList[MathList.Keys.LastOrDefault()];
            }
            return null;
        }

        public static string GetDefaultVersion()
        {
            //IL_0016: Unknown result type (might be due to invalid IL or missing references)
            //IL_001c: Expected O, but got Unknown
            Assembly defaultMath = GetDefaultMath();
            if (DebugMode || defaultMath == null)
            {
                AnalyseMath val = new AnalyseMath();
                return val.Version;
            }
            if (defaultMath != null)
            {
                try
                {
                    Type type = defaultMath.GetType("MyMath.AnalyseMath");
                    object obj = Activator.CreateInstance(type);
                    PropertyInfo property = type.GetProperty("Version");
                    object value = property.GetValue(obj, null);
                    return Convert.ToString(value);
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
            return string.Empty;
        }

        private static Assembly GetCorrectMath(Experiment experiment)
        {
            Assembly assembly = (from s in MathList
                                 where s.Key == experiment.DumpVersion
                                 select s.Value).FirstOrDefault();
            if (assembly == null && MathList.Count > 0)
            {
                return MathList.Values.FirstOrDefault();
            }
            return assembly;
        }

        public static AnalyseError DataAnalyse(Experiment experiment, bool isRealTime)
        {
            //IL_0017: Unknown result type (might be due to invalid IL or missing references)
            //IL_001d: Expected O, but got Unknown
            Assembly correctMath = GetCorrectMath(experiment);
            if (DebugMode || correctMath == null)
            {
                AnalyseMath val = new AnalyseMath();
                val.Experiment = experiment;
                val.DataAnalyse(isRealTime);
                return val.ErrorRecord;
            }
            if (correctMath != null)
            {
                try
                {
                    Type type = correctMath.GetType("MyMath.AnalyseMath");
                    object obj = Activator.CreateInstance(type);
                    PropertyInfo property = type.GetProperty("Experiment", typeof(Experiment));
                    property.SetValue(obj, experiment, null);
                    MethodInfo method = type.GetMethod("DataAnalyse");
                    method.Invoke(obj, new object[1] { isRealTime });
                    property = type.GetProperty("ErrorRecord");
                    object value = property.GetValue(obj, null);
                    return (AnalyseError)((value is AnalyseError) ? value : null);
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        public static string CheckOrder(Experiment experiment)
        {
            //IL_0007: Unknown result type (might be due to invalid IL or missing references)
            //IL_000d: Expected O, but got Unknown
            if (DebugMode)
            {
                AnalyseMath val = new AnalyseMath();
                val.Experiment = experiment;
                return val.CheckOrder();
            }
            Assembly correctMath = GetCorrectMath(experiment);
            if (correctMath != null)
            {
                try
                {
                    Type type = correctMath.GetType("MyMath.AnalyseMath");
                    object obj = Activator.CreateInstance(type);
                    PropertyInfo property = type.GetProperty("Experiment", typeof(Experiment));
                    property.SetValue(obj, experiment, null);
                    MethodInfo method = type.GetMethod("CheckOrder");
                    object obj2 = method.Invoke(obj, new object[0]);
                    return obj2.ToString();
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
            return string.Empty;
        }

        public static void DifferenceCurve(Experiment experiment, List<SampleTargetItem> selectedTargets, HRMRefTarget CurrentHRMTarget)
        {
            //IL_0007: Unknown result type (might be due to invalid IL or missing references)
            //IL_000d: Expected O, but got Unknown
            if (DebugMode)
            {
                AnalyseMath val = new AnalyseMath();
                val.Experiment = experiment;
                val.DifferenceCurve(selectedTargets, CurrentHRMTarget);
                return;
            }
            Assembly correctMath = GetCorrectMath(experiment);
            if (!(correctMath != null))
            {
                return;
            }
            try
            {
                Type type = correctMath.GetType("MyMath.AnalyseMath");
                object obj = Activator.CreateInstance(type);
                PropertyInfo property = type.GetProperty("Experiment", typeof(Experiment));
                property.SetValue(obj, experiment, null);
                MethodInfo method = type.GetMethod("DifferenceCurve");
                method.Invoke(obj, new object[2] { selectedTargets, CurrentHRMTarget });
            }
            catch (Exception)
            {
            }
        }
    }
}
